package com.shopping.base;

import java.lang.reflect.ParameterizedType;

import org.springframework.beans.factory.annotation.Autowired;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.shopping.config.Config;
import com.shopping.model.User;
import com.shopping.service.UserService;

/**
 * This class is used for ...
 * 
 * @author liuhuan
 * @version 1.0, 2013-8-31 下午11:28:09
 */
public class BaseAction<T> extends ActionSupport implements ModelDriven<T> {

	private static final long serialVersionUID = -5401867633834095749L;

	protected UserService userService = null;

	protected T model;

	protected Class<T> modelClass;

	protected User getCurrentUser() {
		return (User) ActionContext.getContext().getSession().get(Config.KEY_USER_SESSION);
	}

	@SuppressWarnings("unchecked")
	public BaseAction() {
		ParameterizedType pt = (ParameterizedType) this.getClass().getGenericSuperclass();
		modelClass = (Class<T>) pt.getActualTypeArguments()[0];
	}

	@Override
	public T getModel() {
		if (model == null) {
			try {
				model = modelClass.newInstance();
			} catch (Exception e) {
				e.printStackTrace();
				throw new RuntimeException(e);
			}
		}
		return model;
	}

	@Autowired
	public void setUserService(UserService userService) {
		this.userService = userService;
	}

}
